/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tactics;

import Game.NewGame;
import IO.Utils;
import Inrterface.Messages;
import Map.MapLevel;
import Monsters.Monster;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author insane
 */
public class TackticUtils {

    public static boolean checkMove(int x, int y) throws FileNotFoundException, IOException, ClassNotFoundException {

        ArrayList monsters = MapLevel.getInstance().getMap().getMonster();
        if(monsters == null) return false;
        for (int i = 0, c = 0; i < monsters.size(); i++) {
            Monster monster = (Monster) monsters.get(i);
            if (monster.getX() == x && monster.getY() == y) {
                int damage = getPlayerDamage();
                monster.setLive(monster.getLive() - damage);
                NewGame.getPlayer().setExp(damage);
                Messages.getInstance().add("player damages " + damage + "("+monster.getLive() +")");
                if (monster.getLive() < 0) {
                    Messages.getInstance().add("player killed " + monster.getName());
                    monsters.remove(i);
                }
                return true;
            }
        }
        return false;
    }

    public static boolean checkMonsterMove(int x, int y, Monster ms) throws FileNotFoundException, IOException, ClassNotFoundException {
        if (TackticUtils.isAttaced(x, y, ms)) {
            return true;
        }
        return checkMove(x, y);
    }

    public static boolean isAttaced(int x, int y, Monster ms) throws FileNotFoundException, IOException, ClassNotFoundException {
        if ((x == MapLevel.getInstance().getMap().x
                && y == MapLevel.getInstance().getMap().y)
                || (x == MapLevel.getInstance().getMap().x - 1
                && y == MapLevel.getInstance().getMap().y - 1)
                || (x == MapLevel.getInstance().getMap().x + 1
                && y == MapLevel.getInstance().getMap().y + 1)
                || (x == MapLevel.getInstance().getMap().x - 1
                && y == MapLevel.getInstance().getMap().y + 1)
                || (x == MapLevel.getInstance().getMap().x + 1
                && y == MapLevel.getInstance().getMap().y - 1)) {
            int damage = NewGame.getPlayer().getHealth();
            NewGame.getPlayer().setHealth(damage - getDamage(ms));
            Messages.getInstance().add(ms.getName() + " damages " + damage);
            return true;
        }
        return false;
    }

    public static int getDamage(Monster ms) {
        int min = ms.getMinDamage();
        int max = ms.getMaxDamage();
        return Utils.rand(min, max);
    }

    public static int getPlayerDamage() {
        return NewGame.getPlayer().getDex() + NewGame.getPlayer().getLevel();
    }
}
